<?php

require_once(dirname(__FILE__).'/../DB/Note.php');
require_once(dirname(__FILE__).'/../DB.php');
require_once(dirname(__FILE__).'/../Note.php');
require_once(dirname(__FILE__).'/../Util.php');

class Note_List {
    public function get_by_member ($member_id, $date = 0, $date_to = 0) {
        if (preg_match('/^\d{4}-\d{1,2}(-\d{1,2})?$/', $date)) {
            $from;
            $to;
            list($y, $m, $d) = explode('-', $date);
            if ($d) {
                $from = $date;
                $to   = $date;
            }
            else {
                $from = sprintf('%04d-%02d-%02d', $y, $m, 1);
                $to   = sprintf('%04d-%02d-%02d', $y, $m, days_in_month($y, $m));
            }
            if (preg_match('/^\d{4}-\d{1,2}(-\d{1,2})?$/', $date_to)) {
                list($y, $m, $d) = explode('-', $date_to);
                if ($d) {
                    $to = $date_to;
                }
                else {
                    $to = sprintf('%04d-%02d-%02d', $y, $m, days_in_month($y, $m));
                }
            }
            return Note_List::get_by_member_date($member_id, $from, $to);
        }

        $db = new DB_Note('slv');
        $raw_list = $db->select_by_member($member_id);
        $list = array();
        foreach ($raw_list as $row) {
            $list[] = new Note($row);
        }
        return $list;
    }

    private function get_by_member_date ($member_id, $from, $to) {
        $db = new DB_Note('slv');
        $raw_list = $db->select_by_member_date($member_id, $from, $to);
        $list = array();
        foreach ($raw_list as $row) {
            $list[] = new Note($row);
        }
        return $list;
    }

    public function get_recent_list ($member_id, $start_date, $offset, $days) {
        if (is_array($member_id)) {
            foreach ($member_id as $id) {
                if (!is_numeric($id))
                    return FALSE;
            }
        }
        else if (!is_numeric($member_id))
            return FALSE;
        else
            $member_id = array($member_id);

        $in = join(', ', $member_id);

        $dbh = DB::load_dbh();
        $query = sprintf("
            SELECT
                start_date
            FROM
                schedules
            WHERE
                member_id IN (%s) AND
                start_date >= '%s' AND
                type = 2
            GROUP BY
                start_date
            ORDER BY start_date
            LIMIT %d, %d",
            $in,
            mysql_real_escape_string($start_date),
            mysql_real_escape_string($offset),
            mysql_real_escape_string($days)
        );

        $result = mysql_query($query);
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }

        $dates = array();
        while ($row = mysql_fetch_assoc($result)) {
            $dates[] = $row['start_date'];
        }

        if(count($dates) > 0)
            $last_key = count($dates) - 1;
        else
            $last_key = 0;

        $query = sprintf("
            SELECT
                *
            FROM
                schedules
            WHERE
                member_id IN (%s) AND
                start_date BETWEEN '%s' AND '%s' AND
                type = 2",
            $in,
            mysql_real_escape_string($dates[0]),
            mysql_real_escape_string($dates[$last_key])
        );

        $result = mysql_query($query);
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }

        $todos = array();
        while($row = mysql_fetch_assoc($result)){
            //preDump($row);
            $time_index = date_to_unix($row['start_date']);
            $member_index = $row['member_id'];
            if (!isset($todos[$time_index][$member_index]['todo'])) {
                $todos[$time_index][$member_index]['todo'] = array();
            }
            $todos[$time_index][$member_index]['todo'][] = $row;
        }

        return $todos;
    }

    public function get_title_count_by_members ($member_id_list) {
        if (!is_array($member_id_list)) {
            $member_id_list = explode(',', $member_id_list);
        }
        $db = new DB_Note('slv');
        $total_list = array();
        foreach ($member_id_list as $member_id) {
            $list = $db->select_title_count_by_member($member_id);
            var_dump($list);
            foreach ($list as $title => $count) {
                if (!isset($total_list[$title])) {
                    $total_list[$title] = 0;
                }
                $total_list[$title] += $count;
            }
        }
        return $total_list;
    }

    public function get_title_count () {
    }

}
